#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>
#include <assert.h>

int checkAdventurer(int treasure1, int treasure2) {

  assert(treasure1 == copper || treasure1 == silver || treasure1 == gold);
  assert(treasure2 == copper || treasure2 == silver || treasure2 == gold);


}

int main (int argc, char** argv) {
  
  for (int n =0; n< 2000; n++) {
  int randomN = rand() % 1000;
  int randomP = rand() % 4;
  struct gameState G;
  int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
           sea_hag, tribute, smithy};
  
  printf ("Starting game.\n");
  
  initializeGame(randomP, k, randomN, &G);
  
  int money = 0;
  int smithyPos = -1;
  int adventurerPos = -1;
  int i=0;

  int numSmithies = 0;
  int numAdventurers = 0;

  while (!isGameOver(&G)) {
    money = 0;
    smithyPos = -1;
    adventurerPos = -1;
    for (i = 0; i < numHandCards(&G); i++) {
      if (handCard(i, &G) == copper)
    money++;
      else if (handCard(i, &G) == silver)
    money += 2;
      else if (handCard(i, &G) == gold)
    money += 3;
      else if (handCard(i, &G) == smithy)
    smithyPos = i;
      else if (handCard(i, &G) == adventurer)
    adventurerPos = i;
    }

    if (whoseTurn(&G) == 0) {
      if (smithyPos != -1) {
        printf("0: smithy played from position %d\n", smithyPos); 
	playCard(smithyPos, -1, -1, -1, &G); 
	printf("smithy played.\n");
	money = 0;
	i=0;
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("0: bought province\n"); 
        buyCard(province, &G);
      }
      else if (money >= 6) {
        printf("0: bought gold\n"); 
        buyCard(gold, &G);
      }
      else if ((money >= 4) && (numSmithies < 2)) {
        printf("0: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
      }
      else if (money >= 3) {
        printf("0: bought silver\n"); 
        buyCard(silver, &G);
      }

      printf("0: end turn\n");
      endTurn(&G);
    }
    else if (whoseTurn(&G) == 1) {
      if (adventurerPos != -1) {
        printf("1: adventurer played from position %d\n", adventurerPos);	
        playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        //assert(drawnTreasure1 == copper && drawnTreasure1 == silver && drawnTreasure1 == gold);
	//assert(drawnTreasure2 == copper && drawnTreasure2 == silver && drawnTreasure2 == gold);
        checkAdventurer(drawnTreasure1, drawnTreasure2);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("1: bought province\n");
        buyCard(province, &G);
      }
      else if ((money >= 6) && (numAdventurers < 2)) {
        printf("1: bought adventurer\n");
	buyCard(adventurer, &G);
	numAdventurers++;
      }else if (money >= 6){
        printf("1: bought gold\n");
	    buyCard(gold, &G);
        }
      else if (money >= 3){
        printf("1: bought silver\n");
	    buyCard(silver, &G);
      }
      printf("1: endTurn\n");
      
      endTurn(&G);      
    }
    else if (whoseTurn(&G) == 2 ) {

if (smithyPos != -1) {
        printf("0: smithy played from position %d\n", smithyPos); 
	playCard(smithyPos, -1, -1, -1, &G); 
	printf("smithy played.\n");
	money = 0;
	i=0;
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("2: bought province\n"); 
        buyCard(province, &G);
      }
      else if (money >= 6) {
        printf("2: bought gold\n"); 
        buyCard(gold, &G);
      }
      else if ((money >= 4) && (numSmithies < 2)) {
        printf("2: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
      }
      else if (money >= 3) {
        printf("2: bought silver\n"); 
        buyCard(silver, &G);
      }

      printf("0: end turn\n");
      endTurn(&G);
	}
  else if (whoseTurn(&G) == 3 ) {
     if (adventurerPos != -1) {
        printf("1: adventurer played from position %d\n", adventurerPos);	
        playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        //assert(drawnTreasure1 == copper && drawnTreasure1 == silver && drawnTreasure1 == gold);
	//assert(drawnTreasure2 == copper && drawnTreasure2 == silver && drawnTreasure2 == gold);
        checkAdventurer(drawnTreasure1, drawnTreasure2);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if (money >= 8) {
        printf("1: bought province\n");
        buyCard(province, &G);
      }
      else if ((money >= 6) && (numAdventurers < 2)) {
        printf("1: bought adventurer\n");
	buyCard(adventurer, &G);
	numAdventurers++;
      }else if (money >= 6){
        printf("3: bought gold\n");
	    buyCard(gold, &G);
        }
      else if (money >= 3){
        printf("3: bought silver\n");
	    buyCard(silver, &G);
      }
      printf("3: endTurn\n");
      
      endTurn(&G);      

}
  } // end of While

  printf ("Finished game.\n");
  //printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, &G), scoreFor(1, &G));

  }
  return 0;
}
